package com.mycar2w.cloud.web.controller;

import com.mycar2w.cloud.entity.ApplieOrder;
import com.mycar2w.cloud.entity.Car;
import com.mycar2w.cloud.entity.CarBasicParameter;
import com.mycar2w.cloud.entity.CarChassisBrakeParameter;
import com.mycar2w.cloud.entity.CarEngineParameter;
import com.mycar2w.cloud.entity.CarInnerConfigutation;
import com.mycar2w.cloud.entity.CarOutConfiguration;
import com.mycar2w.cloud.entity.CarSecurityConfiguration;
import com.mycar2w.cloud.entity.CarSystemTestBrakeEnginecompartment;
import com.mycar2w.cloud.entity.CarSystemTestChassis;
import com.mycar2w.cloud.entity.CarSystemTestIndicatorLight;
import com.mycar2w.cloud.entity.CarTestAccelerate;
import com.mycar2w.cloud.entity.CarTestDecelerationBraking;
import com.mycar2w.cloud.entity.CarTestStart;
import com.mycar2w.cloud.entity.CarTestStartup;
import com.mycar2w.cloud.entity.CarTestUniformDriving;
import com.mycar2w.cloud.entity.TTransaction;
import com.mycar2w.cloud.entity.TUser;
import com.mycar2w.cloud.service.repository.ApplieOrderRepository;
import com.mycar2w.cloud.service.repository.CarRepository;
import com.mycar2w.cloud.service.repository.TUserRepository;
import com.mycar2w.cloud.service.serviceimpl.CarOptionService;
import com.mycar2w.cloud.web.model.TransactionReturn;
import com.mycar2w.cloud.web.util.ReturnModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * created time 2017/3/25
 *
 * @author ljc
 * @version V1.0
 */
@RestController
@RequestMapping("/car_option")
public class CarOptionByPersonalController extends MycarBaseConTroller{
    @Autowired
    private TUserRepository tUserRepository;
    @Autowired
    private CarOptionService carOptionService;
    @Autowired
    private CarRepository carRepository;
    @Autowired
    private ApplieOrderRepository applieOrderRepository;

    /**
     * 申请卖汽车的页面
     * @return
     */
    @RequestMapping(value = "/sale_applie",method = RequestMethod.GET)
    public ModelAndView CarSaleApplied(){
        getMypPhone();
        return ReturnModelAndView.returnSuccess("sale_applie");
    }

    /**
     * 提交卖汽车的申请
     * @param applieOrder
     * @return
     */
    @RequestMapping(value = "/sale_applie_post",method = RequestMethod.POST)
    public ModelAndView CarSaleAppliedPost(@ModelAttribute ApplieOrder applieOrder){
        String phone=getMypPhone();
        TUser tUser=tUserRepository.findByPhone(phone);
        applieOrder.setUserId(tUser.getId());
        applieOrder.setIsOver(0);
        carOptionService.saveApplieOrder(applieOrder);
        return ReturnModelAndView.returnSuccess("congratulation","发起汽车交易申请成功，工作人员将会在最近联系您，请保持电话通畅！");
    }

    /**
     * 查看申请卖汽车的集合
     * @return
     */
    @RequestMapping(value = "/sale_applie_list",method = RequestMethod.GET)
    public ModelAndView CarSearchSaleAppliedListGet(){
        String phone=getMypPhone();
        List<ApplieOrder> applieOrders = carOptionService.findByPhone(phone);
        return ReturnModelAndView.returnSuccess("sale_applie_list",applieOrders);
    }

    /**
     * 查看卖汽车申请的详细信息
     * @param applieOrderId 申请id
     * @return
     */
    @RequestMapping(value = "/sale_applied_info",method = RequestMethod.GET)
    public ModelAndView CarSearchSaleAppliedInfoGet(@RequestParam Long applieOrderId){
        getMypPhone();
        ApplieOrder applieOrder = carOptionService.findByApplieOrderId(applieOrderId);
        return ReturnModelAndView.returnSuccess("sale_applied_info",applieOrder);
    }

    /**
     * 查看售卖汽车的列表
     * @return
     */
    @RequestMapping(value = "/search_sale_list",method = RequestMethod.GET)
    public ModelAndView CarSearchSaleListGet(){
        String phone=getMypPhone();
        List<Car> cars=carOptionService.findAllSaleCarByPhone(phone);
        return ReturnModelAndView.returnSuccess("car_sale_list",cars);
    }
    @RequestMapping(value = "/search_car_info",method = RequestMethod.GET)
    public ModelAndView CarSearchSaleInfoGet(@RequestParam Long carId){
        TUser tUser=carOptionService.getUserByCarId(carId);
        Car car=carOptionService.findByCarId(carId);
        CarBasicParameter carBasicParameter=carOptionService.getCarBasicParameter(carId);
        CarChassisBrakeParameter carChassisBrakeParameter=carOptionService.CarChassisBrakeParameter(carId);
        CarEngineParameter carEngineParameter=carOptionService.getCarEngineParameter(carId);
        CarInnerConfigutation carInnerConfigutation=carOptionService.getCarInnerConfigutation(carId);
        CarOutConfiguration carOutConfiguration=carOptionService.getCarOutConfigutation(carId);
        CarSecurityConfiguration carSecurityConfiguration=carOptionService.getCarSecurityConfiguration(carId);
        CarSystemTestBrakeEnginecompartment carSystemTestBrakeEnginecompartment=carOptionService.getCarSystemTestBrakeEnginecompartment(carId);
        CarSystemTestChassis carSystemTestChassis=carOptionService.getCarSystemTestChassis(carId);
        CarSystemTestIndicatorLight carSystemTestIndicatorLight=carOptionService.getCarSystemTestIndicatorLight(carId);
        CarTestAccelerate carTestAccelerate=carOptionService.getCarTestAccelerate(carId);
        CarTestDecelerationBraking carTestDecelerationBraking=carOptionService.getCarTestDecelerationBraking(carId);
        CarTestStart carTestStart=carOptionService.getCarTestStart(carId);
        CarTestStartup carTestStartup=carOptionService.getCarTestStartup(carId);
        CarTestUniformDriving carTestUniformDriving=carOptionService.getCarTestUniformDriving(carId);
        Map<String,Object> result=new HashMap<>();
        result.put("tUser",tUser);
        result.put("car",car);
        result.put("carBasicParameter",carBasicParameter);
        result.put("carChassisBrakeParameter",carChassisBrakeParameter);
        result.put("carEngineParameter",carEngineParameter);
        result.put("carInnerConfigutation",carInnerConfigutation);
        result.put("carOutConfiguration",carOutConfiguration);
        result.put("carSecurityConfiguration",carSecurityConfiguration);
        result.put("carSystemTestBrakeEnginecompartment",carSystemTestBrakeEnginecompartment);
        result.put("carSystemTestChassis",carSystemTestChassis);
        result.put("carSystemTestIndicatorLight",carSystemTestIndicatorLight);
        result.put("carTestAccelerate",carTestAccelerate);
        result.put("carTestDecelerationBraking",carTestDecelerationBraking);
        result.put("carTestStart",carTestStart);
        result.put("carTestStartup",carTestStartup);
        result.put("carTestUniformDriving",carTestUniformDriving);
        return ReturnModelAndView.returnSuccess("sale_car_info",result);
    }
    @RequestMapping(value = "/transac",method = RequestMethod.GET)
    public ModelAndView CarSearchTransactionalListGet(@RequestParam(defaultValue = "0") Integer pageIndex){
        String phone=getMypPhone();
        List<TTransaction> tTransactions=carOptionService.transactionListGet(phone,pageIndex);
        List<TransactionReturn> transactionReturns=tTransactions.stream().map(tTransaction -> {
            TransactionReturn transactionReturn=new TransactionReturn();
            transactionReturn.setBuyPhone(tUserRepository.findById(tTransaction.getBuyUserId()).getPhone());
            transactionReturn.setSalePhone(tUserRepository.findById(tTransaction.getSaleUserId()).getPhone());
            transactionReturn.setSaleMoney(Double.parseDouble(carRepository.findById(tTransaction.getCarId()).getTrueMoney()));
            transactionReturn.setCarId(tTransaction.getCarId());
            transactionReturn.setCarXinghao(carRepository.findById(tTransaction.getCarId()).getAppliedOrderId().getCarXinghao());
            transactionReturn.setSaleTime(tTransaction.getCreateTime());
            return transactionReturn;
        }).collect(Collectors.toList());
        return ReturnModelAndView.returnSuccess("transaction_list",transactionReturns);
    }


}
